<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Page;

/**
 * 文章控制器
 * @author jry <598821125@qq.com>
 */
class CourseController extends AdminController
{
   /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    public $tag1 = array('1'=>'超高端沙龙','2'=>'品牌沙龙','3'=>'妈妈班','4'=>'mini课程','5'=>'品牌活动','6'=>'多维整合集市','7'=>'直播','8'=>'原创视频','9'=>'线下活动出席','10'=>'kol代言','11'=>'自媒体合作','12'=>'企业团建(含实景植入)');
    public function index()
    {
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group');
        // 搜索
        $keyword         = I('keyword', '', 'string');
        $condition       = array('like', '%' . $keyword . '%');
        //$map['name|mobile'] = array($condition, $condition, '_multi' => true);


        $map['status'] = array('egt', '0'); // 禁用和正常状态

        $post_object   = D('Admin/Course');

        $p             = !empty($_GET["p"]) ? $_GET['p'] : 1;


        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $post_object   = D('Admin/Course');
        $data_list     = $post_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );
        
        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('列表') // 设置页面标题
            ->addTopButton('addnew', array('href' => U('add'))) // 添加新增按钮

                ->addRightButton('edit') // 添加编辑按钮
            ->addRightButton('resume') // 添加启用按钮
            ->addRightButton('forbid') // 添加禁用按钮
            ->addRightButton('delete') // 添加删除按钮
           // ->SetTabNav($tab_list,1)
            //->setTemplate(APP_PATH . 'Common/Builder/Layout/Admin/nlist.html')
            ->addTableColumn('id', 'ID')
            ->addTableColumn('tag1txt', '项目类别')
            ->addTableColumn('part2', '项目主题')
            ->addTableColumn('create_time', '创建时间', 'time')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')

            ->setTableDataList($data_list) // 数据列表
            ->display();
        
    }


    public function add()
    {
        $uid = session('user_auth.uid');
        if (IS_POST) {
            $post_object = D('Admin/Course');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->add();
                if ($id) {

                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            $order_object = D('Admin/Course');
            $map['status'] = array('egt', '0'); // 禁用和正常状态


            // 使用FormBuilder快速建立表单页面。

            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增课件') // 设置页面标题
                ->setPostUrl(U('add')) // 设置表单提交地址
                ->addFormItem('part1','select','项目类别','项目类别',$this->tag1)
                ->addFormItem('part2','textarea','项目主题','项目主题')
                ->addFormItem('part3','files','策划','策划')
                ->addFormItem('part4','files','执行方案','执行方案')
                ->addFormItem('part5','files','执行流程','执行流程')
                ->addFormItem('part6','files','课件','课件')
                ->addFormItem('part7','files','话术','话术')
                ->addFormItem('part8','files','物料清单','物料清单')
                ->addFormItem('part9','files','课程表','课程表')
                ->addFormItem('part10','files','整体数据','整体数据')
                ->addFormItem('part11','files','照片','照片')
                ->addFormItem('part12','files','视频','视频')
                ->addFormItem('part13','files','项目总结','项目总结')
                ->display();
        }
    }
    /**
     * 编辑文章
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        $uid = session('user_auth.uid');
        if (IS_POST) {
            $post_object = D('Admin/Course');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->save();
                if ($id !== false) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $data = D('Admin/Course')->find($id);
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑课件') // 设置页面标题
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('part1','select','项目类别','项目类别',$this->tag1)
                ->addFormItem('part2','textarea','项目主题','项目主题')
                ->addFormItem('part3','files','策划','策划')
                ->addFormItem('part4','files','执行方案','执行方案')
                ->addFormItem('part5','files','执行流程','执行流程')
                ->addFormItem('part6','files','课件','课件')
                ->addFormItem('part7','files','话术','话术')
                ->addFormItem('part8','files','物料清单','物料清单')
                ->addFormItem('part9','files','课程表','课程表')
                ->addFormItem('part10','files','整体数据','整体数据')
                ->addFormItem('part11','files','照片','照片')
                ->addFormItem('part12','files','视频','视频')
                ->addFormItem('part13','files','项目总结','项目总结')
                ->setFormData($data)
                ->display();
        }
    }



    // 将树形列表转换为树
    public function list_as_tree($list, $extra = null, $key = 'id', $title_field = 'title')
    {
        //转换成树状列表(非严格模式)
        $tree = new \Util\Tree();
        $list = $tree->array2tree($list, $title_field, 'id', 'pid', 0, false);

        if ($extra) {
            $result[0] = $extra;
        }

        //转换成一维数组
        foreach ($list as $val) {
            $result[$val[$key]] = $val['title_show'];
        }
        return $result;
    }
}
